﻿using System;
using System.Collections.Generic;

namespace Fuse8.ContentManagementFramework.Web.FileMinifier
{
    public class CompareFileInfoEntries : IComparer<string>
    {
        public int Compare(string firstFileName, string secondFileName)
        {
            int firstFileNameNumber = 0;
            int secondFileNameNumber = 0;

            int index = firstFileName.IndexOf('-');
            if (index > 0)
            {
                int.TryParse(firstFileName.Substring(0, index), out firstFileNameNumber);
            }

            index = secondFileName.IndexOf('-');
            if (index > 0)
            {
                int.TryParse(secondFileName.Substring(0, index), out secondFileNameNumber);
            }

            return (firstFileNameNumber > 0) && (secondFileNameNumber > 0) ?
                firstFileNameNumber.CompareTo(secondFileNameNumber) :
                string.Compare(firstFileName, secondFileName);
        }
    }  
}
